package springboot.demo.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import springboot.demo.dao.DepartmentDao;
import springboot.demo.dao.EmployeeDao;
import springboot.demo.mapper.adminMapper;
import springboot.demo.pojo.Department;
import springboot.demo.pojo.Employee;

import javax.servlet.http.HttpSession;
import java.util.Collection;
import java.util.List;

@Controller
public class EmployeeController {
    @Autowired
    EmployeeDao employeeDao;
    @Autowired
    DepartmentDao departmentDao;
@Autowired
adminMapper adminMapper;

    @RequestMapping("/emps")
    public String employeeController(Model model) {
        Collection<Employee> employees = employeeDao.getAll();
        model.addAttribute("emps", employees);

//        List<Employee> employeeList = adminMapper.querryEmpList();
//for (Employee employee:employeeList){
//    System.out.println(employee);
//}
//model.addAttribute("emps",employeeList);
        return "emp/list";
    }



    //显示部门列表
    @GetMapping("/emp")
    public String showEmployee(Model model) {

        Collection<Department> departments = departmentDao.getdepartment();
        model.addAttribute("deps", departments);
        return "emp/add";
    }

    @PostMapping ("/emp")
    public String addEmployee(Employee employee) {
employeeDao.save(employee);
//增加一个员工信息
        return "redirect:/emps";
    }
    //实现员工信息的修改
@GetMapping("/toupdate/{id}")
 public String toupdate(@PathVariable("id") Integer id, Model model){
    Employee employee = employeeDao.getEmployeeById(id);
    model.addAttribute("emp",employee);
    //要注意把这个deps传给前端
    Collection<Department> departments = departmentDao.getdepartment();
    model.addAttribute("deps", departments);
    return"emp/updateEmp";
 }

//修改员工信息的实现
    @PostMapping("/update")
    public  String update(Employee employee){
        //重复保存一次就是修改
        employeeDao.save(employee);
        return "redirect:/emps";
    }
    //删除员工功能的实现
    @RequestMapping("/delete/{id}")
    public  String delete(@PathVariable("id")Integer id){
        employeeDao.deleteEmployeeById(id);
        return "redirect:/emps";
    }
    //注销掉用户
    @RequestMapping("/loginout")
    public  String loginout(HttpSession session){
        session.invalidate();
        return "redirect:/index.html";
    }



}
